<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎注册网上书城</view>
			<uni-forms ref="form" :modelValue="formData" :rules="rules" validateTrigger="bind">
				<uni-forms-item label="昵称" name="name">
					<input type="text" class="input" v-model="formData.name" placeholder="请输入昵称"
						@blur="binddata('name',$event.detail.value)" />
				</uni-forms-item>
				<uni-forms-item label="邮箱" name="email">
					<input class="input" v-model="formData.email" type="text" placeholder="请输入邮箱"
						@blur="binddata('email',$event.detail.value)" />
				</uni-forms-item>
				<uni-forms-item label="密码" name="password">
					<input class="input" v-model="formData.password" type="password" placeholder="请输入密码"
						@blur="binddata('password',$event.detail.value)" />
				</uni-forms-item>
				<uni-forms-item label="确认密码" name="password_confirmation" :errorMessage="errorMessage">
					<input class="input" v-model="formData.password_confirmation" type="password" placeholder="请确认密码"
						@blur="binddata('password_confirmation',$event.detail.value)" />
				</uni-forms-item>
			</uni-forms>
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">注册</button>


			<!-- <input class="u-border-bottom" v-model="name" placeholder="请输入昵称" />
			<input class="u-border-bottom" v-model="email" placeholder="请输入邮箱帐号" />
			<input class="u-border-bottom" type="password" v-model="password" placeholder="请输入密码" />
			<input class="u-border-bottom" type="password" v-model="password_confirmation" placeholder="请确认密码" />
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">注册</button> -->
			<view class="alternative">
				<view class="password">忘记密码</view>
				<navigator class="issue" url="./login">登录帐号</navigator>
			</view>
		</view>
		<view class="buttom">
			<view class="loginType">
				<view class="wechat item">
					<view class="icon">
						<u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon>
					</view>
					微信
				</view>
				<view class="QQ item">
					<view class="icon">
						<u-icon size="70" name="qq-fill" color="rgb(17,183,233)"></u-icon>
					</view>
					QQ
				</view>
			</view>
			<view class="hint">
				注册代表同意
				<text class="link">网上书城用户协议、隐私政策。</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					name: '',
					email: '',
					password: '',
					password_confirmation: ''
				},
				errorMessage: '',
				rules: {
					// 对name字段进行必填验证
					name: {
						rules: [{
								required: true,
								errorMessage: '请输入昵称',
							},
							{
								minLength: 3,
								maxLength: 6,
								errorMessage: '昵称长度在 {minLength} 到 {maxLength} 个字符',
							}
						]
					},
					// 对email字段进行必填验证
					email: {
						rules: [{
							required: true,
							errorMessage: '请输入邮箱地址',
						}, {
							format: 'email',
							errorMessage: '请输入正确的邮箱地址',
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入密码',
						}]
					},
					password_confirmation: {
						rules: [{
							required: true,
							errorMessage: '请输入确认密码',
						}]
					}
				}
			}
		},
		computed: {
			inputStyle() {
				let style = {};
				if (uni.$u.test.email(this.formData.email) && this.formData.password && this.formData
					.password_confirmation && (this.formData.password == this
						.formData.password_confirmation) && this.formData.name) {
					style.color = "#fff";
					style.backgroundColor = uni.$u.color['warning'];
				}
				return style;
			}
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					//说明数据验证通过，接下来还要判断一下密码和确认密码是否相同
					if (res.password != res.password_confirmation) {
						this.errorMessage = '两次输入的密码不相同';
					} else {
						//数据都OK了，可以注册
						console.log('可以注册');
						this.register(res);
					}
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			async register(info){
				let res = await uni.$u.http.post('/api/auth/register', {
					name: info.name,
					email: info.email,
					password: info.password,
					password_confirmation: info.password_confirmation
				});
				//如果登录失败，会返回false
				if (res === false) {  //因为注册成功，API并没有值返回，所以不能直接判断是不是空。但如果出错，API拦截器会返回false。
					return;
				}				
				uni.$u.toast('注册成功');
				setTimeout(()=>{
					uni.$u.route({
						type:'redirect',  
						url:'./login'
					})
				},1500)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input {
		width: 100%;
		font-size: 16px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
		padding: 0 10px;
		height: 35px;
	}

	.getCaptcha {
		margin: 40rpx 0;
	}

	.u-border-bottom,
	.hint {
		margin-bottom: 40rpx !important;
	}

	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}

		.buttom {
			.loginType {
				display: flex;
				padding: 350rpx 150rpx 150rpx 150rpx;
				justify-content: space-between;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28rpx;
				}
			}

			.hint {
				padding: 20rpx 40rpx;
				font-size: 20rpx;
				color: $u-tips-color;

				.link {
					color: $u-type-warning;
				}
			}
		}
	}
</style>
